<?php

namespace Admin\Controller;

use Think\Image;
use Think\Upload;

class GoodController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 商品首页
     * Author:边
     */
    public function index()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $where = '';
        } else {
            $where['m.store_id'] = $store_id['store_id'];
        }

        $keyword = I('request.keyword');
        if ($keyword) {
            $where['m.name'] = array('like', "%$keyword%");
        }
        $status = I('request.status');
        if ($status) {
            if ($status != -1) {
                $where['is_up'] = $status;
            }
        }

        $count = M('dishes')->alias("m")->where($where)->count(); // 查询满足要求的总记录数

        $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出
        $result = M('dishes')->alias("m")
            ->field(array("m.*,mdc.name as category_name,s.store_name"))
            ->join("meal_dishes_category as mdc on m.category_id=mdc.id", 'left')
            ->join("meal_store as s on m.store_id=s.store_id", 'left')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("m.id desc")
            ->select();
        foreach ($result as $key => $value) {
            $result[$key]['create_formart_time'] = date('Y-m-d', $value['create_time']);
            if ($value['modify_time']) {
                $result[$key]['modify_formart_time'] = date('Y-m-d', $value['modify_time']);
            } else {
                $result[$key]['modify_formart_time'] = '暂无修改';
            }
        }
        $this->assign("status", $status);
        $this->assign("keyword", $keyword);
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("result", $result);
        $this->display();
    }

    /**
     *商品增加修改加载页面
     * Author:边
     */
    public function add_edit()
    {
        $store_id = session("wadmin");
//print_r($store_id);

        if ($store_id['store_id'] == 0) {
            $where = '';
        } else {
            $where['store_id'] = $store_id['store_id']; // 店铺ID
        }

        $id = I('get.id');
        $dishes = M('dishes')->where("id='$id'")->find();
//        $dishes['menu'] = M('dishes_category')->where($data)->select();
        $category = M('dishes_category')->where($where)->select();
        $store = M('store')->select();

//        echo "<pre>";
//        print_r($category);

//        echo "<pre>";
//        print_r($dishes);

        $this->assign('result', $dishes);
        $this->assign('category', $category);
        $this->assign('storeId', $store_id['store_id']);
        $this->assign('store', $store);
        $this->display();
    }

    /**
     * 商品增加修改
     * Author:边
     */
    public function editGood()
    {
        $store_id = session("wadmin");

        $id = I('request.id');
        $data['create_time'] = time();
        $data['category_id'] = I('post.category_id');
        $data['description'] = I('post.description');
        $data['food_material'] = I('post.food_material');
        $data['name'] = I('post.name');
        $data['original_price'] = I('post.original_price');
        $data['price'] = I('post.price');
        $data['num'] = I('post.num');
        $data['is_up'] = I('post.is_up');
        if ($store_id['store_id'] == 0) {
            $data['store_id'] = I('post.store_id');
        } else {
            $data['store_id'] = $store_id['store_id'];
        }
        if ($_FILES ['img'] ['name'][0] !== '') {
            $files['img'] = $_FILES ['img'];
            $img1 = $this->upload($files);
            $picarr1 = array();
            foreach ($img1 as $key1 => $val1) {
                array_push($picarr1, ltrim($val1[savepath], ".") . $val1[savename]);
            }
            $data['image'] = implode(',', $picarr1);
        }
        if ($id) {
            //修改
            $data['modify_time'] = time();
            if (trim(I('post.modifier'))) {
                $data['modifier'] = I('post.modifier');
            } else {
                $data['modifier'] = $store_id['user_name'];
            }
            $result = M('dishes')->where("id='$id'")->save($data);
        } else {
            // 新增
            $data['creater'] = $store_id['user_name'];
            $result = M('dishes')->add($data);
        }
        if ($result) {
            $this->success("操作成功", U('Admin/Good/index'));
        } else {
            $this->error("操作失败", U('Admin/Good/index'));
        }

    }

    /**
     * 菜品属性列表
     */
    public function attributes_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = ''; // 店铺ID
        } else {
            $data['a.store_id'] = $store_id['store_id']; // 店铺ID
        }
        $dish = M('attributes')->alias("a")->field("a.*,s.store_name")->join("meal_store as s on a.store_id=s.store_id")->where($data)->order('a.id desc')->select();
        $this->assign('result', $dish);
        $this->display();
    }

    /**
     * 增加菜品属性页面
     */
    public function add_attributes()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = ''; // 店铺ID
            $storeId = 0;
        } else {
            $data['store_id'] = $store_id['store_id']; // 店铺ID
            $storeId = $store_id['store_id'];
        }

        //查询菜品的种类
        $id = I('get.id');
        $data['id'] = $id;
        $dish = M('attributes')->where($data)->find();
        $store = M('store')->select();
        $this->assign('result', $dish);
        $this->assign('store', $store);
        $this->assign('storeId', $storeId);
        $this->display();
    }

    /**
     * AJAX->菜品(每个分类下的菜品)
     */
    public function get_dish()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = '';
        } else {
            $data['store_id'] = $store_id['store_id']; // 店铺ID
        }

        //查询菜品
        $categoryId = I('post.category');
        $data['category'] = $categoryId;
        $dish = M('dishes')->where($data)->select();

        $this->ajaxReturn(array('state' => 1, 'dish' => $dish), 'json');
    }

    /**
     * 增加菜品属性
     */
    public function editAttributes()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data['store_id'] = I('post.store_id'); // 获取的店铺ID
        } else {
            $data['store_id'] = $store_id['store_id']; // 储存的店铺ID
        }
        $id = I('post.id');
        if ($id) {
            //编辑
            $data = I('post.');
            $data['modify_time'] = time();
            $data['store_id']=$store_id['store_id'];
            $data['modifier'] = $store_id['user_name'];
            $result = M('attributes')->where("id='$id'")->save($data);
        } else {
            //新增
            $data = I('post.');
//            dump($data);die;
            $data['store_id']=$store_id['store_id'];
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();

            $result = M('attributes')->add($data);
        }
        if ($result !== false) {
            $this->success("操作成功", U('Admin/Good/attributes_list'));
        } else {
            $this->error("操作失败", U('Admin/Good/attributes_list'));
        }
    }

    /**
     * 删除商品
     */
    public function delGood()
    {
        $id = I('get.id');
        //查找该菜品的分类
        if ($id) {
            $result = M('dishes')->delete($id);
            if ($result) {
                $this->success("删除成功", U('Admin/Good/index'));
            } else {
                $this->error("删除失败", U('Admin/Good/index'));
            }
        }
    }

    /**
     * 删除属性
     */
    public function delAttributes()
    {
        //获取id 属性下面有值的话不能删除
        $attributeId = I('get.id');
        $dishes = M('dish_attributes')->where("attribute_id='$attributeId'")->find();
        if ($dishes) {
            $this->error("该属性下面有值,不能删除!", U('Admin/Good/attributes'));
        } else {
            $result = M('attributes')->delete($attributeId);
            if ($result) {
                $this->success("删除成功", U('Admin/Good/attributes'));
            } else {
                $this->error("删除失败", U('Admin/Good/attributes'));
            }
        }
    }

    /**
     * 删除属性值
     */
    public function delValue()
    {
        //该属性是不是被菜品绑定
        $attributes_value = I('get.id');
        $is_attribute = M('dish_attributes')->where('FIND_IN_SET(' . $attributes_value . ',attribute_value)')->select();

        if ($is_attribute) {
            $this->error("该分类绑定菜品了,不能删除!", U('Admin/Good/attributes_value_list'));
        } else {
            $result = M('attributes_value')->delete($attributes_value);
            if ($result) {
                $this->success("删除成功", U('Admin/Good/attributes_value_list'));
            } else {
                $this->error("删除失败", U('Admin/Good/attributes_value_list'));
            }
        }
    }

    /**
     * 绑定菜品属性页面
     */
    public function select_attributes()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $map = '';
        } else {
            $map['store_id'] = $store_id['store_id']; // 店铺ID
        }

        //获取所有属性名称
        $id = I('get.id');
        $result = M('dish_attributes')->where("id='$id'")->find(); //菜品属性表

        $store = M('store')->where($map)->select();
        $this->assign('store',$store ); //店铺

        $category = M('dishes_category')->where($map)->select();
        $this->assign('category', $category); //菜名分类表

        $dish = M('attributes')->where($map)->select();
        $this->assign('dish', $dish); // 菜的属性名表

        if ($id) {
            $values = M('dishes')->field('id,category_id,name')->where($map)->select();
            $this->assign('values', $values); // 菜名表
        }

        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 增加，编辑绑定菜品属性
     */
    public function save_attributes()
    {
        $store_id = session("wadmin");
        $id = I('post.id');
        if ($id) {
            //编辑
            $data = I('post.');
            $data['modify_time'] = time();
            $data['modifier'] = $store_id['user_name'];
            $result = M('dish_attributes')->where("id='$id'")->save($data);
        } else {
            //新增
            $data = I('post.');
//            dump($data);die;
            //判断是否重复添加菜品的名称属性
            $attribute_id = $data['attribute_id'];
            $attribute_value = $data['attribute_value'];//属性值id
            $dish_id = $data['dish_id'];
            //去掉点号
//            $dish_result = M('dish_attributes')->where("attribute_id='$attribute_id' and dish_id='$dish_id'")->find();
            $dish_result = M('dish_attributes')->where("dish_id='$dish_id' and attribute_value='$attribute_value' ")->find();
            if ($dish_result) {
                $this->error("不能重复的相同的属性！", U('Admin/Good/select_attributes'));
            } else {
                $data['creater'] = $store_id['user_name'];
                $data['create_time'] = time();
                $result = M('dish_attributes')->add($data);
            }
        }
        if ($result !== false) {
            $this->success("操作成功", U('Admin/Good/select_attributes_list'));
        } else {
            $this->error("操作失败", U('Admin/Good/select_attributes_list'));
        }
    }

    /**
     * 菜品属性的列表
     */
    public function select_attributes_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $where = '';
        } else {
            $where['v.store_id'] = $store_id['store_id'];
        }
        //菜品的属性列表
        $result = M('dish_attributes')->alias("d")
            ->join("join meal_dishes as v on d.dish_id=v.id")
            ->join("meal_store as s on v.store_id=s.store_id")
            ->join('meal_attributes c ON c.id = d.attribute_id')
            ->field('d.id as id,d.dish_id as dish_id,v.name as name,v.store_id,s.store_name,d.attribute_value_name as attribute_value_name,d.attribute_value as attribute_value,
                d.is_need as is_need,c.name_attribute as attribute_name,d.attribute_value_price as attribute_value_price,d.creater,d.create_time')
            ->where($where)
            ->order('d.id desc')
            ->select();
        foreach ($result as &$v) {
            if ($v['is_need'] == '1') {
                $v['is_need_display'] = "可选";
            } else {
                $v['is_need_display'] = "必选";
            }
        }

        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 获取菜品的属性值
     */
    public function add_attributes_value()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = ''; // 店铺ID
            $storeId = 0;
        } else {
            $data['store_id'] = $store_id['store_id']; // 店铺ID
            $storeId = $store_id['store_id'];
        }

        $id = I('get.id');
        $result = M('attributes_value')->where("id='$id'")->find();
        //菜品的种类
        $category = M('attributes')->where($data)->select();
        $store = M('store')->select();
        $this->assign('category', $category);
        $this->assign('result', $result);
        $this->assign('storeId', $storeId);
        $this->assign('store', $store);
        $this->display();
    }

    /**
     * 菜品的属性列表
     */
    public function attributes_value_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = ''; // 店铺ID
        } else {
            $data['a.store_id'] = $store_id['store_id']; // 店铺ID
        }

        //获取所有的菜品的分类及数值
        $result = M('attributes')->alias("a")
            ->field("a.*,v.*,s.store_name")
            ->join("join meal_attributes_value as v on a.id=v.attribute_id")
            ->join("meal_store as s on a.store_id=s.store_id")
            ->order('attribute_id desc')
            ->where($data)
            ->select();
        $this->assign('result', $result);
        $this->display();
    }

    /**
     * 根据属性id获取属性值
     */
    public function get_attributes()
    {
        $attributeId = I('post.attributeId');
        $result = M('attributes_value')->where("attribute_id='$attributeId'")->select();

        $this->ajaxReturn(array('state' => 1, 'result' => $result), 'json');
    }

    /**
     * 保存菜品的属性值
     */
    public function save_values()
    {
        $id = I('post.id');
        $store_id = session("wadmin");
        if ($id) {
            //编辑
            $data = I('post.');
            $data['modify_time'] = time();
            $data['modifier'] = $store_id['user_name'];
            $result = M('attributes_value')->where("id='$id'")->save($data);
        } else {
            //新增
            $data = I('post.');
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();
            if ($store_id['store_id'] == 0) {
                $data['store_id'] = I("post.store_id");
            } else {
                $data['store_id'] = $store_id['store_id']; // 店铺ID
            }
            $result = M('attributes_value')->add($data);
        }
        if ($result !== false) {
            $this->success("操作成功", U('Admin/Good/attributes_value_list'));
        } else {
            $this->error("操作失败", U('Admin/Good/attributes_value_list'));
        }
    }


    /**
     * 绑定菜品的属性
     */
    public function selectAttributes()
    {
        $store_id = session("wadmin");
        $id = I('post.id');
        if ($id) {
            //编辑
            $data = I('post.');
            $data['modify_time'] = time();
            $data['modifier'] = $store_id['user_name'];
            $result = M('dish_attributes')->where("id='$id'")->save($data);
        } else {
            //新增
            $data = I('post.');
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();
            $result = M('dish_attributes')->add($data);
        }
        if ($result !== false) {
            $this->success("操作成功", U('Admin/Good/attributes_value'));
        } else {
            $this->error("操作失败", U('Admin/Good/attributes_value'));
        }
    }

    /**
     * 删除绑定的属性
     */
    public function delSelectAttributes()
    {

        $id = I('get.id');

        $result = M('dish_attributes')->delete($id);
        if ($result) {
            $this->success("删除成功", U('Admin/Good/select_attributes_list'));
        } else {
            $this->error("删除失败", U('Admin/Good/select_attributes_list'));
        }
    }

    /*
     * ajax获取店铺名称
     */
    public function ajax_storeCategory()
    {
        $store_id = I('get.store_id');
        $category = M('dishes_category')->field('id,name')->where(['store_id' => $store_id])->select();
        $data = [];
        foreach ($category as $key => $value) {
           $data[$value['id']]=$value['name'];
        }
        $this->ajaxreturn(array('state' => 1, 'msg' => '请求成功', 'data' => $data), 'json');
    }
}